from d115.mailman.models import *
from django.utils.encoding import smart_unicode
from django.core.mail import send_mail
from django.template import Context, Template
from django.conf import settings

def send_email(tname, cntxt, rcpts):
    if type(tname) in [str, unicode]:
        try:
            tmpl = EmailTemplate.objects.get(name=tname)
        except:
            return False
    else:
        tmpl = tname
    context = Context(cntxt)
    subject = smart_unicode(Template(tmpl.subject).render(context))
    body = smart_unicode(Template(tmpl.message).render(context))
    rcpts = rcpts if type(rcpts)==list else [rcpts, ]
    send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, rcpts, fail_silently=True)
